package cn.jsu.oj.dispatchServer.handle.handles.checkinfohandles;

import cn.jsu.oj.dispatchServer.handle.CommandPack;
import cn.jsu.oj.dispatchServer.handle.handles.Handle;
import cn.jsu.oj.dispatchServer.init.JudgeClient;
import cn.jsu.oj.dispatchServer.parsing.HandleType;
import cn.jsu.oj.dispatchServer.pojo.checkinfo.JudgeAbstractCheckInfo;
import cn.jsu.oj.dispatchServer.resource.Resource;
import cn.jsu.oj.dispatchServer.thread.DispatchThread;
import lombok.extern.slf4j.Slf4j;


/**
 * @author 24987
 */
@Slf4j
@HandleType(type = 93)
public class JudgeCheckInfoHandle implements Handle {


    @Override
    public CommandPack<?> handle(CommandPack<?> pack) {
        JudgeAbstractCheckInfo checkInfo = (JudgeAbstractCheckInfo) pack.getData();
        if (checkInfo.getPassword().equals(Resource.JUDGE_PASSWORD)) {
            JudgeClient judgeClient = new JudgeClient(checkInfo.getMappingQueueLength());
            Resource.storeTaskMap.put(pack.getIp(), judgeClient.getStoreMap());
            DispatchThread.addJudgeClient(judgeClient);
            log.info("判题机验证密码成功 IP-PORT:" + pack.getIpAndPort());
            return new CommandPack<>(pack.getIp(), judgeClient);
        }
        log.info("判题机验证密码错误 IP-PORT:" + pack.getIpAndPort());
        return null;
    }
}
